#include "stuwindow.h"
#include "ui_stuwindow.h"

StuWindow::StuWindow(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StuWindow),
    m_recordPage(nullptr),
    m_borrowPage(nullptr),
    m_returnPage(nullptr)
{
    ui->setupUi(this);

    /* 设置图标 */
    setWindowIcon(QIcon(":/pic/lib.jpg"));

    /* 设置标题 */
    setWindowTitle("图书管理系统/学生");

    /* 页面初始化 */
    initPage();

    connect(this, &StuWindow::stulogin, m_recordPage, &stu_record::getStuname);
    connect(this, &StuWindow::stulogin, m_borrowPage, &stu_borrow::getStuname);
    connect(this, &StuWindow::stulogin, m_returnPage, &stu_return::getStuname);
    connect(this, &StuWindow::close, m_recordPage, &stu_record::clearView);
    connect(this, &StuWindow::updataView, m_borrowPage, &stu_borrow::updataView);
    connect(this, &StuWindow::updataView, m_recordPage, &stu_record::updataView);
}

/* 学生界面关闭事件 */
void StuWindow::closeEvent(QCloseEvent *event)
{
    emit close();
}

/* 点击借阅记录界面按钮 */
void StuWindow::on_recordBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(0);
    emit updataView();
}

/* 点击借书界面按钮 */
void StuWindow::on_borrowBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
    emit updataView();
}

/* 点击还书界面按钮 */
void StuWindow::on_returnBtn_clicked()
{
    ui->stackedWidget->setCurrentIndex(2);
}

void StuWindow::getStuname(QString name)
{
    m_stuName = name;
    emit stulogin(m_stuName);
}

/* 页面初始化 */
void StuWindow::initPage()
{
    m_recordPage = new stu_record(this);
    m_borrowPage = new stu_borrow(this);
    m_returnPage = new stu_return(this);
    ui->stackedWidget->addWidget(m_recordPage);
    ui->stackedWidget->addWidget(m_borrowPage);
    ui->stackedWidget->addWidget(m_returnPage);
    ui->stackedWidget->setCurrentIndex(0);
}

StuWindow::~StuWindow()
{
    delete ui;
}
